﻿using Flurl.Http;
using OkFlurl;
using System.Text.Json;
using System.Text.Json.Nodes;

namespace Test
{

    class pp
    {
        public DateTime AddTime { get; set; }
    }

    internal class Program
    {
        static async Task Main(string[] args)
        {
            FlurlExt.SetDefaultJsonSerializer();

            var qqq = new FlurlRequest("http://www.baidu.com");

            await FlurlExt.GetJsonAsync<JsonObject>(qqq, new { a = 1, Name = "李思思", c = DateTime.Now });

            var req = new FlurlRequest("https://www.baidu.com");
            var bytes = new byte[] { 1, 2, 3, 4 };
            var d = await FlurlExt.PostAsync(req, bytes);

            //var url = new FlurlRequest("http://www.baidu.com");
            //var content = new FlurlStreamContent(async (stream) =>
            //{
            //    await stream.WriteAsync(new byte[] { 1, 2, 3, 4 });
            //});
            //await FlurlExt.PostReturnJsonAsync<JsonObject>(req, content);

            var json = JsonSerializer.Serialize(new pp { });
            json = """
                {"AddTime":null}
                """;
            Console.WriteLine(json);
            var pppp = JsonSerializer.Deserialize<pp>(json, FlurlExt.JsonOptions);
            return;

            var url = "https://pics0.baidu.com/feed/f603918fa0ec08fa82fd8b393277786057fbdad2.jpeg?token=a4ad50eb633955c5da143b63f685e4c7";
            //url = "http://download.osgeo.org/postgis/windows/pg16/postgis-bundle-pg16x64-setup-3.4.1-1.exe";
            //url = "http://10.10.53.2:5293/update/update.zip";
            //url = "http://10.10.53.2:5293/login/getakka";
            //var req = new FlurlRequest(url);
            //var progress = new FlurlProgress((total, downloadLen, percent) =>
            //{
            //    Console.WriteLine($"文件大小:{total},当前已下载:{downloadLen}");
            //    Console.WriteLine("\r%{0:N4}", percent * 100);

            //});

            //var fullPath = await FlurlExt.GetDownloadDiskAsync(req, null, @"C:\\A", null, progress);
            //Console.WriteLine(fullPath);

            //using var fs = new FileStream(@"D:\WPS_Setup_14309.exe", FileMode.Open);


            var req2 = new FlurlRequest("http://localhost:5212/test/Upload111");
            var form = new FlurlForm
            {
                UploadBufferSize = 10,
                DownloadBufferSize = 10
            };

            form.UploadProgress = new FlurlProgress(data =>
            {
                Console.WriteLine($"文件大小:{data.Total},当前已上传:{data.Read},==文件名{form.UploadProgress.FileName},==>{data.Percent}%");
                //Console.WriteLine("\r%{0:N4}", data.Percent);
            });

            form.DownloadProgress = new FlurlProgress(data =>
            {
                Console.WriteLine($"文件大小:{data.Total},当前已下载:{data.Read},==文件名{form.DownloadProgress.FileName},==>{data.Percent}%");
                //Console.WriteLine("\r%{0:N4}", data.Percent);
            });

            form.AddFile("a", @"C:\Users\Administrator\Desktop\dddd2.txt");

            var par = new { id = "123", name = "李四123abcjadj" };

            await FlurlExt.FormDownloadDiskAsync(req2, par, @"C:\\AA2", null, form);


            Console.WriteLine("Hello, World!");
            Console.ReadKey();

        }
    }
}
